<?php

namespace Biz\model\bizAd;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class AdWallet extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_biz_ad';

    const _CONFIG_SELECT_ = 'db_biz_ad';

    const _DATABASE_ = 'biz_ad';

    const _TABLE_NAME = 'hhz_ad_wallet';

    const WALLET_DEFAULT_BALANCE = 0.00;

    const WALLET_DEFAULT_VERSION = 0;

    use BizModelTrait;

    /**
     * 获取用户钱包
     * @param $uid
     * @return array|false|mixed
     */
    public static function getWallet($uid)
    {
        $where = [
            'uid' => $uid,
            'data_status' => self::STATUS_NORMAL
        ];

        $wallet = self::dataSelectInfo($where);

        if ($wallet && is_array($wallet)) {
            return $wallet;
        }

        return false;
    }

    /**
     * 获取用户钱包余额
     * @param $uid
     * @return float|mixed|null
     */
    public static function getWalletBalance($uid)
    {
        $where = [
            'uid' => $uid,
            'data_status' => self::STATUS_NORMAL
        ];

        $balance = self::dataValue($where, 'balance');

        $balance = $balance > 0 ? $balance : self::WALLET_DEFAULT_BALANCE;

        return $balance;
    }


    /**
     * 创建钱包
     * @param $uid
     * @param $utype
     * @param $balance
     * @return false|array
     */
    public static function createWallet($uid, $utype, $balance = self::WALLET_DEFAULT_BALANCE)
    {
        $data = [
            'uid' => $uid,
            'utype' => $utype,
            'balance' => $balance,
            'lock_amount' => 0.00,
            'create_time' => date('Y-m-d H:i:s', time()),
            'version' => self::WALLET_DEFAULT_VERSION,
            'data_status' => self::STATUS_NORMAL,
        ];

        $id = self::dataInsert($data);

        if ($id) {
            $data['id'] = $id;
            return $data;
        }

        return false;

    }

    /**
     * 充值
     * @param $walletId
     * @param $balance
     * @param $newBalance
     * @param $version
     * @param $newVersion
     * @return bool|int
     */
    public static function recharge($walletId, $balance, $newBalance, $version, $newVersion)
    {

        $updateData = [
            'balance' => $newBalance,
            'version' => $newVersion,
            'update_time' => date('Y-m-d H:i:s', time())
        ];

        $where = [
            'id' => $walletId,
            'balance' => $balance,
            'version' => $version,
            'data_status' => self::STATUS_NORMAL
        ];

        return self::dataUpdate($updateData, $where, true);
    }

    /**
     * 扣款
     * @param $walletId
     * @param $balance
     * @param $newBalance
     * @param $lockAmount
     * @param $newLockAmount
     * @param $version
     * @param $newVersion
     * @return bool|int
     */
    public static function deduct($walletId, $balance, $newBalance, $lockAmount, $newLockAmount, $version, $newVersion)
    {
        if ($newBalance < 0) {
            return false;
        }

        $updateData = [
            'balance' => $newBalance,
            'lock_amount' => $newLockAmount,
            'version' => $newVersion,
            'update_time' => date('Y-m-d H:i:s', time())
        ];

        $where = [
            'id' => $walletId,
            'version' => $version,
            'lock_amount' => $lockAmount,
            'balance' => $balance,
            'data_status' => self::STATUS_NORMAL
        ];

        return self::dataUpdate($updateData, $where, true);
    }

    /**
     * 结算在途金额
     * @param $walletId
     * @param $balance
     * @param $newBalance
     * @param $lockAmount
     * @param $newLockAmount
     * @param $version
     * @param $newVersion
     * @return bool|int
     */
    public static function settleLockAmount(
        $walletId,
        $balance,
        $newBalance,
        $lockAmount,
        $newLockAmount,
        $version,
        $newVersion
    ) {
        $updateData = [
            'balance' => $newBalance,
            'lock_amount' => $newLockAmount,
            'version' => $newVersion,
            'update_time' => date('Y-m-d H:i:s', time())
        ];
        $where = [
            'id' => $walletId,
            'version' => $version,
            'balance' => $balance,
            'lock_amount' => $lockAmount,
            'data_status' => self::STATUS_NORMAL
        ];
        return self::dataUpdate($updateData, $where, true);
    }

    /**
     * 钱包提现
     * @param $walletId
     * @param $balance
     * @param $newBalance
     * @param $version
     * @param $newVersion
     * @return bool|int
     */
    public static function cashOut($walletId, $balance, $newBalance, $version, $newVersion)
    {
        if ($newBalance < 0) {
            return false;
        }

        $updateData = [
            'balance' => $newBalance,
            'version' => $newVersion,
            'update_time' => date('Y-m-d H:i:s', time())
        ];

        $where = [
            'id' => $walletId,
            'version' => $version,
            'balance' => $balance,
            'data_status' => self::STATUS_NORMAL
        ];

        return self::dataUpdate($updateData, $where, true);
    }


}